/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fch.sdlibrary.service.impl;

import com.fch.sdlibrary.entity.Book;
import com.fch.sdlibrary.entity.User;
import com.fch.sdlibrary.mapper.BookMapper;
import com.fch.sdlibrary.mapper.HistoryMapper;
import com.fch.sdlibrary.mapper.UserMapper;
import com.fch.sdlibrary.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author chnewei
 */

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;
    @Resource
    BookMapper bookMapper;
    @Resource
    HistoryMapper historyMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户
        User user = userMapper.findOneByUserName(username);
        if (user == null) {
            throw new UsernameNotFoundException("身份错误！"); //抛出异常
        }
        return user;
    }

    public User insertUser(User user) {
        //设置默认角色
        if (user.getUserRole() == null || "".equals(user.getUserRole())) {
            user.setUserRole("user");
        }
        userMapper.insertUser(user);
        User fullUser = userMapper.findOneByUserName(user.getUserId());
        return fullUser;
    }

    /**
     * 是有借阅
     *
     * @param userId
     * @return
     */
//    @Override
//    public boolean hasBorrow(String userId) {
//        return userMapper.hasBorrow(userId) != null;
//    }

    @Override
    public Book hasBorrow(String userId) {
        return userMapper.hasBorrow(userId);
    }

    /**
     * 借阅
     *
     * @param bookId
     * @param userId
     * @return
     */
    @Override
    public boolean doBorrow(String bookId, String userId) {
        if (userMapper.doBorrowFromUser(bookId, userId) > 0) {
            if (bookMapper.doBorrowFromBook(bookId) > 0) {
                historyMapper.addBorrowHit(userId, bookId);
                return true;
            }
            else
                userMapper.doBorrowFromUser("0", userId);
        }
        return false;
    }

    /**
     * 还书
     * @param userId
     * @return
     */
    @Override
    public boolean doRemand(String userId) {
        Book book = userMapper.hasBorrow(userId);
        if (userMapper.doRemand(userId) > 0) {
            if (bookMapper.doRemandFromBook(book.getBookId().toString()) > 0) {
                historyMapper.addRemandHit(userId, book.getBookId().toString());
                return true;
            }
            else
                userMapper.doBorrowFromUser(book.getBookId().toString(), userId);
        }
        return false;
    }

}